optimized version of bubbles