Refactor string comparision code