improve algorithm and decrease storage size