Refactor mail processing into module