Updated with exception handling