untested almost working version