Refactor IMAP