further code simplification with state machine and progress on correctness of the buffer sequence