code style fixes, better error handling