Matrix error refactoring and cleanup