Javascript function getting called twice