Added more error-handling to code.