Converted to rudimentary state machine