Various cleanups, optimizations