Cleaning up some code for efficiency (in editing and performance)

Changed how the regex expressions are assembled. Moved link replacing into a function. Moved/changed/added several variables and comments for clarity.