cleaning up some of the code and added some more comments