Improve form html generation

Still not creating valid html but fixes a few bits and the show hide bug reported in #971