BS-2419
Fix dependency removal algorithm