Updated javascript to be less repeat-y