Algorithm refactoring, matrix renamed to grid