Better error handling in main read loop