Improve readline history logic