Cleaned up a bunch of redundant or obsolete code and added some more comments.