refactored to fit in the engine