Optimized cod and bug fix