queue: some refactoring on Handler.loop

Make it clever and removed some duplicate code.