Fixed bug, added comments and removed unecessary code