added a check that headers array is set before using it