Some refactoring of the shadow code