improved exception handling
minor refactoring