Added test case and improved exception handling