Implement functional tests. Minor refactoring. Add 404 template.