refactoring and commenting for readability