Add better validation logic and logging.