cleanup and add logging