Fixed a lot more bugs and refactored the form control functions.