Optimization: use references in loops to prevent copying [do not merge before pedal fix]