refactored to simplify the code