more exception catching and minor refactoring