Optimize with_metadata if statement