Added JSDoc, refactored few methods, see diff