Many bug fixes and enhancements