fixed entity count
trying to fix entity count
Cleaned the code and added comments