Moved mail functions out