added a lot of comments and documentation. Using jsDoc to generate nice document pages from comments.