improved imap.py