Bug fixes and some clean up in js