refactor background.js and catch unwanted exceptions