added some code and fixed crashes

changed Density calculation code