various bug fixes (unit tests all pass now)