Cleaned up a bunch of code and added more informative comments.