fixed a bug in the plain javascript version.