Refactoring code to improve readability