Moved initial generation of space into a function