Why immutable queue? I think using a mutable queue here actually makes your control flow much simpler