expand() replaced with more optimal version