javascript code cleanup + fixing syntax errors