move display flush into a function