misc bug fixes and improve JS compatbility