Made the javascript code more automatic