add javascript invoke; reorganize to make it a bit easier to follow