Refactored some codes in order to autogenerate emails