Fixed bug and cleaned up some code based upon feedback from Thotaka.