Optimize data structure and code