code refactored to endless loop model