mass refactoring in progress; it compiles, but logic is still bad