Fix error handling that got broken at some point, refactored keep-alive stuff