Improved Javascript code