Optimize using sets as functions