refactor, using 1d array instead of 2d array