Refactor subnet and sg extraction into functions