Fixed state machine, now fully implemented