Changed all int[][] and double[][] to short[][] for efficiency