Removed console debug output.  Rearanged code for readability