polished up error handling and fixed minor problems