Added code to handle background execution properly