Some simplifications and added error output.