move trim algorithm into reference