Lots of code cleanups