maybe refactor this block as a private method, since the same code is copied a few lines down