refactored form submit function