Add mmap calls, and simplify logic