Restructure, add logging