refactored and tested in clear environment