Added comments, finished the state machine implementation