Javascript bugs almost fixed