source code reorganize, move some generic funtions to separate file.