trying to make it exit at the right time