DOCS-454 refactor

needs thorough testing, and I think the javascript should probably be even more clean in pursuit of maintainability.