refactored action handling code; fixed foreground issue