Refactor form selection code