js code refactored