modularize the mail listener, and other small changes