Refactored mail download loop, better error handling