Email detection and formatting logic improved