Removed some debug code, added comments, wrote additional documentation.