fixed the code up and added comments