improved logic and functionality of the fsm