Added error-checking, issue #4