Added end condition check - fixes #7.