Get bug fixes from original repo

Bug fixes and tweaks from original repo.