Refactor leaf reading to squash some bugs