Added jsdoc
Optimized to reuse more code