Optimize code and update comments