optimiziation in visualization code