Various bug fixes and enhancements