refactored to make code more adaptable for different files