few bug corrections on matrix and ref, added tests for matrix