Fixed timing mismatch between Python and Javascript.
Cleaned up the js code.