Move javascript inside functions