refactored and peer reviewed