Major refactor plus JSDoc