tested matrix funcs and optimized for more readability