Refactoring email code