Changed exception handling to improve error recognition