fixing bugs, reducing memory usage