fix logic errors in tuning code (but it's still sloppy)