Minor bugfixes and rearranged