fix better algorithm
use numpy