First pass at writing down logic and state machine