General bug fixes to pass tests