Moved URL build code to top of page, seems to fail in some situations when in bottom. fixes #6