Improve trim function