Factored out nasty directory handling

It's a mess because we can't assume we are under the document/web root.