added input safeguards