Moved duplicate javascript code to the one place