Refactor method of Queue to reduce duplicted code