seems mostly working, but not totally idempotent and needs testing...