Many bug fixes and upgrades