refactored to make functions work better