lots of tweaks and bug fixes