adding some error catching, changing printing encode for stdout piping