Added input checking, some comments.