pls refactor,  if- else at the begging and then other logic