Updated re-factor with tests