Extract removeLastCharacter function and add test