Bug fixes

Improved checking to ensure that we get only the lines we want
from our output, presence of headers is handled better, and much
more.