Moving metadata stuff inside isPrimary? check; also, update_post_meta() should use $post_id as arg, not $id.