Fix syntax errors

Started using JSLint. Moved handlers into separate file.