Cleaned up the code and updated the comments