Prevent some crashes and handle EOS properly