buggy implementation for finding terminal contacts