Background scripts: main.js refactoring

Split into files.