refactored and added logs