Refactor mail loop