Fix for the case of extended trims
Code cleanup