Cleaned up tree_farm and moved common functions to API