fixed seg fault from mem map; fixed inifinte loop issue