refactored mailing logic to fix a logical bug #286