How to get the animals in Minecraft to stay in their pen?

So for some reason the animals manage to get out of their pens and I can't seem to figure out what to do to keep them in.

Either:
Dig the floor one down
Make the fence two high
Make sure the area is big enough so the animals don't glitch out

Shut the door. Don't put blocks inside the pen. Make the walls thicker and taller. Cows can climb ladders.