43-58: duplicated logic, perhaps move to a function?