Untangling your Logic using State Machines