Improved order of statemachine for performance I hope