updated code for density estimation