little restructuring and fixes