The puzzle of the emergence of cooperation between unrelated individuals is shared across diverse fields of behavioural sciences and economics. In this article we combine the public goods game originating in economics with evolutionary approaches traditionally used in biology. Instead of pairwise encounters, we consider the more complex case of groups of three interacting individuals. We show that territoriality is capable of promoting cooperative behaviour, as in the case of the Prisoner's Dilemma. Moreover, by adding punishment opportunities, the readiness to cooperate is greatly enhanced and asocial strategies can be largely suppressed. Finally, as soon as players carry a reputation for being willing or unwilling to punish, highly cooperative and fair outcomes are achieved. This group–beneficial result is obtained, intriguingly, by making individuals more likely to exploit their co–players if they can get away with it. Thus, less–cooperative individuals make more-cooperative societies.