fix cross-platform bugs, make work in webkit