added type-aware attachment logic