Fix bug in expansion algorithm