It might make sense to refactor this into a small private static method, as this logic is used in other places (like line 100).