more error handling and minor refactorings