Working out scope tree