diff --git a/lib/priority_queue.rb b/lib/priority_queue.rb
index 9f2272f75835cee34ff84c958cc67aaf8242fe6c..0d77512d5061c6ec4b620d23a0e9fd266cc6bd1f 100644
--- a/lib/priority_queue.rb
+++ b/lib/priority_queue.rb
@@ -22,7 +22,7 @@ class PriorityQueue
     min = @elements[1]
 
     # and make sure the tree is ordered again
-    bubble_down(1)
+    bubble_down(1) unless empty?
     min
   end
 
@@ -62,7 +62,7 @@ class PriorityQueue
       child_index = (index * 2)
 
       # stop if we reach the bottom of the tree
-      if child_index > @elements.size - 1
+      if child_index >= @elements.size
         @elements[index] = target
         return
       end