Numerous bug fixes and code cleanup/enhancements'