Added checks for invalid input