further optimize to only flatten once per heartbeat