refactor background.js to have more DRY code