added plot and flip detection