Implmentation of engine, refactoring