             len += LZMAConstants.matchMinLen
             if uncompressedSize > -1 && uncompressedSize < len { throw LZMAError.RepeatWillExceed }
             for _ in 0..<len {
-                let byte = out[rep0 + 1 <= dictEnd ? dictEnd - rep0 - 1 : dictionarySize - rep0 - 1 + dictEnd]
-                out.append(byte)
+                let byte = self.byte(at: rep0 + 1)
                 uncompressedSize -= 1
-                dictEnd += 1
-                if dictEnd - dictStart == dictionarySize {
-                    dictStart += 1
-                }
-            }
-        }
-    }
-    
+                self.put(byte)
+            }
+        }
+    }
+
+    // MARK: Dictionary (out window) related functions.
+
+    private func put(_ byte: UInt8) {
+        out.append(byte)
+        dictEnd += 1
+        if dictEnd - dictStart == dictionarySize {
+            dictStart += 1
+        }
+    }
+
+    private func byte(at distance: Int) -> UInt8 {
+        return out[distance <= dictEnd ? dictEnd - distance : dictionarySize - distance + dictEnd]
+    }
+
 }

                    if rangeDecoder.decode(bitWithProb: &probabilities[241 + (state << LZMAConstants.numPosBitsMax) + posState]) == 0 {
                        // SHORT REP MATCH CASE
                        state = state < 7 ? 9 : 11
                        let byte = out[rep0 + 1 <= dictEnd ? dictEnd - rep0 - 1 : dictionarySize - rep0 - 1 + dictEnd]
                        out.append(byte)
                        uncompressedSize -= 1
                        dictEnd += 1
                        if dictEnd - dictStart == dictionarySize {
                            dictStart += 1
                        }
                        continue
                    }
                } else { // REP MATCH CASE


                // DECODE LITERAL:
                /// Previous literal (zero, if there was none).
                let prevByte = dictEnd == 0 ? 0 : self.byte(at: 1)
                /// Decoded symbol. Initial value is 1.
                var symbol = 1
                /**
