Code refactoring (JS and templates)