Refactored the engine code