This is duplicated above, you should refactor this into a shared function.