fixed index out of bounds on the right and bottom edges