some reordering and bugfixes