KMP refactor