combine two nearly identical functions into one