Add existence checks for $input