Code optimization  & bug fixes