this snippet is duplicated below, perhaps make it a method?