Refactor form code