Updated with more error handling.