split some code out to separate functions