this really should be extracted into a tested helper method