Moved function outside of form submission handler