Add mail function and minor cleanups