Separate functions would work almost identical, so might as well merge into single function and generalize it