Major bug fixes and minor refinements.