Improve path trim function