moved the code around and added in log event for sending emails