Refactored entity state handling.