Refactored email function.