Add input validity check and check for recursive flag