adds error handling, ready for MVC refactor