Improving Javascript and moving jQuery code into Javascript file