Fix if perm bug, add tests