Newest version

implements FSM and much more logic.