Cleaned up loop in pin interrupt and timer interrupt; Reorganized structure of state machine