Add utility routines and use them to simplify the use of dynamically allocated memory. Signed-off-by: Dan Cross <patchdev@fat-dragon.org>