refactored to improve readability, added debugging statement