Few important bug fixes in main JS script