Fix handling of several edge cases.