Refactor tree and cartesian code into subdirs