more cleanup and optimization