WTE: make algorithm more efficient