some code refactory based on jshint tips