Refactor run-length encoding script