Tested more thoroughly; Added some comments and removed some redundant code