Updated for defensive error checks