Optimizing string manipulation a bit