fixed indexing errors and fixed how histogram is calculated. Able to handle non-square image tiles