Now the entity handling works as intended. Some cleanup as well