lots of tiny tweaks of code structure