Cleaned up the code, added comments, removed unused exception class.