Javascript was weirdly nested within one big function within ready, is not any more