fixes
first reasonably working version
more refac