Refactor background.js functions