refactored to make the code cleaner