Debugged the crap out of the js file. Program works robustly