added more nil checks to protect