Fix bug in error calculation