added some error handleing and input checking