backported refactor from email_in branch