Refactored foreground.js