some restructuring and a bugfix