clean up code and added some comments