Refactor matrix initialization