Fix handling of edge cases in readline(), other small improvements