Update tests to work in Firefox

This PR also contains a few bug fixes which the tests helped me to spot. :-)