Refactor things so they are a little bit cleaner.  Also use perl instead of sed
for non-greedy regex expressions.