Feature/js refactoring

Mostly performance issues and code cleanup.