improved code, packed dual function for forest