Added JSDoc and some refactoring