cleaning up code and adding more comments