refcounts for realms, rewritten some locking and subrealm stuff