Added slightly better error handling