Rewrite and completed the code for historybufferReadPrevious and historyBufferReadNext function