added some better error handling