truly idempotent
fixed a few bugs, added stricter checking