Made error handling more dynamic.