Cleaned up the code, and added error trapping, and support for dynamic 404 errors.