lots of code cleanup