Cleaned up code and added comments.