multiple code optim & bug fixes