Implemented more thorough exception handling