move imap logout to before the redirect to make sure we cleanly terminate