Made some basic refactoring of the cleaning function for the references.
Refactored the reference creation.