Refactored trim function and moved to util.[hc]