correct logic errors (issue #5258)