fixed the optimal calculation bug; can now auto adapt.