make matching algorithm work with mmap and fix bugs