tool_operate: Moved main initialisation and cleanup code into tool_main