Replace fprintf with write + move declares around