updated imap logic