Simplified and rewrite the code for historyBufferReadPrevious and historyBufferReadNext function, fully tested the 2 functions