Simplifying and refactoring tree.c to understand it better. encode.c is next!