minor optimizations and some cleanup