engine: refactor engine logic