smarter trim implementation