little refactoring of js code