fixing numerous bugs from refactoring