refactor js code and put debug message