improve error metrics
clean up code