Use a priority queue to avoid linear lookups for the smallest element. Also cache the priority queue instead of creating a new on every time.