moved alert check into if statement - not tested, so not sure if it works