Moved javascript to separate file. Changed timing of query execution to prio rendering the page.