Added proper exception handling