small tweaks to make the code more robust and report errors better