code refactor of email controller