Major refactoring to allow combining index and key-value matching together. Bugfixes and performance improvements