Added some error checking and output to state machine