fix memory usage on large matricies