code refactored and jsdoc added