Javascript improvements suggested by @saivann (thanks!)