Exit function when conditions met