Resolve memory management issues with handler by freeing handler earlier and using static callback