refactored to be simpler