some reordering
simplified snippet