fix lots of bugs caused during refactor