Started rewriting some functions, now uses more recursion.