Fixes stylesheet reference when hosting in non-root folder, Refactor to reduce repeated code, improved query string processing

* Fixes broken stylesheet reference when hosing in a non-root folder by adding '~' to the path.
* Use default arguments to reduce repeated code
* Parameter checking
* Improved method of parsing and reassembling of query strings