Cleaned up code and added lots of comments