cleaned up code and add comments