Moved tilde expansion into its own function