Refactoring of main.js to make theme a bit more robust