Updated to handle exceptions better