more cleanup and some tiny optimizations