Cleaned up code, added comments, etc.