added (hopefully) better error handling