more refactoring, still kludgy in many places. some bugfixes also