moved error inside if statement