Improved JavaScript code