Update engine.rb

I've done a bit more of refactoring.