cleaned up code and add comments to document the code