Added some more doucmentation and refactored.