semi-working version

works better than the older version