Refactored codes. Still WIP for Email