Looking at the code again, the first thing I'd do is to get rid of all those unnecessary pointers.