Code cleanup and validating fixes

Cleanup of the PHP code.
Fixing the form validations, which were wrong in some cases.