Some initial work on tidying up the JS conversion. Lots more to do.