speedup code a bit