Renamed and rewritten functions for cleanup in engine.