Added states and Reworked code