some rearrangement and rbm_K with scan (less mem expensive)