refactor message handling in background.js