Add mail tests and clean up code