refactored to make code clearer