Refactor out type conversion code