Moved the replacement code into separate function.

Can be refactored further to make it easy to add multiple map layers.