refactored code & improved performance