improved pretty printing loop