Refactor out ast creating code