bug fixes by Jaume to prevent loops in getAllReplacements