Added more input checking