Simplify the entity code and fix memory leak
Fix uninitialized var