refactors code to implement  Nimit's changes