Code cleanup and plot optimization