Moves shadow creation to a function.
Merges with master.