error handling around curl, code style and small refactors