Moved the bucket initialization to the outside of the bucket handling loop