Refactored submit form function