refactor matrix walk