Lots of code cleanup