Implement new email functionality and do some small refactoring