Add tests and fixes for applyPatches()