Add comments to javascript and refactor some functionality.

Remove unecessary return statements. DRY up some functions. Remove unecessary functions and calls.