Improve expand()
Improve error detection