Moved some code around and updated and wrote some jsdoc documentation.