Added more optimization. Fixed JS bugs