Refactor foreground masking