Improved "Modified run-length encoding" solution -- a tail-recursive version