Sync

a few optimizations and fixes from core.