Extract background code. Shuffle around some functions.