Form cleanup

Various bug fixes and best-practice cleanups in the form template.

(Note: I didn't try running this yet; please make sure I didn't accidentally break anything.)