make error handling consistent

Still need to handle errors properly and exit with the correct code.