Refactor toMatrix() and allow to convert stacks into matrices
Add unit tests for toMatrix() helper function