add missing header <cstddef> <tuple>, change to function template, and some fixes.