cleaned up and reorderded code