error and disconnect handling, minor refactoring
v1.0