Move render and write to the fd to a separate function