Remove unused code, move some functions to be static