This paper proposes a perturbation-based algorithm to solve models with both idiosyncratic and aggregate risk and a distribution of agents. The algorithm is described with an application to a version of the canonical model of Krusell and Smith (1998), which features aggregate and idiosyncratic risk, a continuum of agents, and incomplete markets. It proceeds by representing the equilibrium conditions of the model as a system of functional equations, discretizing and linearizing these equations, and solving the resulting finite-dimensional linear model using standard methods. We discuss theoretical convergence properties of the method and present numerical results.