Lots of changes + some code cleanup