Fix memory access bug and improve scaling