some rearranging, bugfixes, tests