Added output filename parameter
Added missing file descriptors.