Refactor code and use the new state functions.
Refactor code and use the new state functions.

copyliu/osmium,wffurr/osmium,copyliu/osmium,osmium-org/osmium,wffurr/osmium,copyliu/osmium,wffurr/osmium,osmium-org/osmium,osmium-org/osmium,osmium-org/osmium,wffurr/osmium