fix JSLint problems, separate code into a new function