added some better exception handling