bug fixes, better error handling