much cleanup and almost completed fsm