Lots of fixes and code clean-up.