“If you think life’s a vending machine where you put in virtue and you get out happiness,” a character on the TV show “Six Feet Under” once noted, “then you’re probably going to be disappointed.” Most people find this out the hard way. I suspect you did. The times when you obeyed all the rules and got punished anyway, ate righteously and still got sick, worked yourself half to death to achieve a goal only to feel depletion and disappointment rather than the happiness you expected — the happiness you paid for, by God!

For thousands of years, wise observers have pointed out that whatever’s in charge of the universe “maketh his sun to rise on the evil and on the good, and sendeth rain on the just and on the unjust.” And for thousands of years, the rest of us have answered: “Wait — what?” No matter how routinely it happens, we’re shocked and appalled to see good folks shivering in downpours of ill fortune while their villainous, luxuriantly tanned enemies send postcards from sunbaked beaches.

Perhaps this indignation arises from some innate sense of justice. That’s what the French doctor Jean-Marc Gaspard Itard started testing in 1801, when he took on the care of Victor, a “wild child” who’d spent an estimated seven of his first 12 years in the woods (being raised by wolves … or squirrels — we’ll never really know). Victor had only a rudimentary understanding of human language and social convention. Yet when Itard experimented by punishing him for behavior that usually earned him a reward, the poor child struggled mightily against his punishment.

Whether or not we’re born with it, we’re certainly socialized into the belief that the nickels and dimes of virtuous acts will drop snack-size potato chip bags of happiness into our lives. Our parents offer praise for obedience; our bosses give productive employees promotions and unproductive ones pink slips; our courts (at least try to) punish misbehavers and recompense the wronged. And of course, an endless stream of books, movies and TV shows offers us narratives in which the good guys win, over and over, while the bad guys ingloriously fail.

No wonder we’re stunned when we follow the path of compliance into catastrophe. This doesn’t feel like bad luck; it’s like an unfathomable malfunction that, in the words of Anne Lamott, “would make Jesus want to drink gin straight out of the cat dish.” While we’re coping with our misfortune — the cancer, the divorce, the bankruptcy — we may also lose faith in the basic rightness of life itself. Some of us spend years kicking the cosmic vending machine, raging at anyone (parents, psychiatrists, lovers, politicians) who might be in a position to cough up the happiness we’ve paid for, or at least give us our money back.

Of course, like any impossible task, this effort produces only exhaustion and despair. At some point, even the strongest burn out. Maybe you’ve reached this limit, plunging from violent outrage into numb nonresistance. Or perhaps — especially if your suffering has been intense — you’ve tried to find relief. Maybe you finally went to a therapist, or learned to meditate, or found yourself downward-dogging away in the yoga pants you swore you’d never wear.

These kinds of responses teach us to stop attacking the imaginary vending machine; instead, we sit down beside it, in all our furious disappointment, waiting for new insight to arise. And here’s the thing — though this approach won’t prevent a lifelong do-gooder from getting cancer or keep a lightning bolt from striking down the nicest person in town, that insight will arise. Following paths of stillness and curiosity, rather than rage and despair, we eventually make a fascinating discovery: Although the righteousness-reward theory hasn’t worked for us so far, it contains a bright thread of truth. There is a kind of virtue that really does buy happiness. The problem is, we’ve been taught to use the wrong kind of virtue and expect the wrong kind of reward. We’ve been plunking euros into a machine geared for dollars, expecting chocolate from a machine that contains only fresh fruit. To get the machine functioning correctly, we need to tweak a couple of definitions.

Let’s consider virtue first. Most of us have been taught that it’s a trait synonymous with adherence to social rules: our family’s way of loving, our peer group’s way of achieving, our social class’s politics and manners. We think it’s virtuous to do what other people want. We may believe this so deeply that we override our innate inclinations. Psychologist Stanley Milgram famously designed a study in which a researcher instructed ordinary people to administer painful electric shocks to a fellow study participant (actually there were no shocks; the subject was an actor pretending to be in pain). Even when they heard a fellow human screaming and begging for mercy, many people went on shocking him just because a scientist intoned, “The experiment requires that you continue.” Following rules? Not always virtuous, it seems.

Now consider happiness. You may believe it comes from external phenomena: praise, money, status, adoration. But even when we attain these things, the happiness they create is temporary and unreliable. External rewards may bring a surge of elation, but it quickly fades, leaving the millionaire still fearing financial loss, the beloved actor still suicidal, the aging supermodel still hating her body. If you’ve experienced this pattern — effort, followed by achievement, followed by elation, followed by a letdown — you may still be tenaciously striving, thinking just a little more money, fame or beauty will make you happy. Observe the evidence. Save your strength.

Try this: Define virtue as living in perfect alignment with what you most deeply feel to be true, and happiness as an upwelling of joy that arises directly from this alignment, regardless of external factors. Then run your own experiment. With these new definitions, you’ll find that the virtue-in, happiness-out vending machine works. It really, truly does.

I’ve experienced this myself. Several times, I’ve broken the rules of my culture to follow my sense of truth — ended my marriage, left my church, chosen to be vocal in my dissent. Each time, I’ve lost relationships and money, experienced social shaming, even suffered threats to my life and liberty. I won’t lie: It hurt. A lot. Yet, paradoxically, each choice also increased a flow of happiness that seemed to arise for no reason except that I’d stopped blocking it. I was amazed to feel peace trickling through sorrow and disappointment, gradually dyeing everything some shade of happy.

When people experience this — despite outward losses — they begin blooming like flowers, from misery to surrender to thoughtfulness to inner peace. Alignment in, joy out. In sickness and in health, for richer and for poorer, that machine works.

If you’d like to experience this for yourself, join me in something I call an Integrity Cleanse. The word integrity (from integer) means “wholeness.” Living in integrity means expressing and doing what’s true for you in all situations. Depart from your truth in any way—offer a fake smile, flatter your awful boss, marry for money—and you become two people: the truth knower and the lie actor. That’s duplicity. And duplicity, not social noncompliance, is the real enemy of joy.

To start the Integrity Cleanse, first ask yourself, “Where am I out of integrity?” Where are you not feeling what you feel, knowing what you know, saying what you believe and doing what feels most right? Once you’ve identified the duplicity, come back into integrity. Speak your truth. Act on it. No matter what.

Sound radical? It is. Plop integrity into an unfair system, and you’ll get back disapproval or attack. People have been imprisoned for living with integrity. People have died for it (sometimes moving society a little closer to equality and liberty in the process). Even if your consequences are relatively minor — your parents object when you leave graduate school, your book group mocks your political stance — they’ll still sting. At first you may feel the same old outrage: “I put in virtue and got back punishment!” Stay the course. See what happens.

I’ve watched many people take Integrity Cleanses. They often leave (or experience rejection in) situations that don’t match their truth. This can feel like the end of the world — because it is: the end of the illusory world where rule following buys happiness. Refusing to give a drug-addicted loved one more money; quitting the secure, horrible job; stating your beliefs to bigots — such actions may feel like dropping atom bombs on your own safety. You’ll certainly be afraid. Maybe sad and angry as well. But almost immediately, you’ll also feel an indescribable relief, as if a broken bone that healed badly has been reset in its correct alignment.

Continue your Integrity Cleanse and you’ll begin to see how the cosmic vending machine really works. You’ll find ways of thriving in the world as yourself, not someone else’s puppet. Despite all the challenges, that will feel good. In fact, it will feel amazing.

But don’t take my word for it. Try putting complete integrity into the vending machine of your own life, and sample what you get back. Even though the reward may not be what you expect, and although some bitterness may mingle with the sweetness of living your truth, I doubt you’ll ever have tasted anything quite so delicious.