Aside from that, here's a bug:

if (total >= 4) return;

This is a classical Error 040 since you sometimes optimize Trail, sometimes not.