improved density function