a number of small fixes, plus: don't call reset on a possibly empty array
ditto - better to check whether we have anything, then return the last item
this is definitely an array, empty() is safe for use and faster
basic stuff here
if we're already trim()ming, we might as well use the result