adapted to newest libs
flush when needed
formatting