Electronic structure of narrow gap semiconductors : understanding gap formation and thermoelectric properties