refactoring to more reliably handle form submits