Added code to handle exceptions