restructuring and minor bugfixes