Cleaned up code a little more, added more comments and documentation.