More efficient sample algorithm