Move common code into separate function. Also, this function is a little smarter than the code it replaced. (grafted from 025f9bcff0014dc47b1d1494358382366d766d87)
Move common code into separate function.
Also, this function is a little smarter than the code it replaced.
(grafted from 025f9bcff0014dc47b1d1494358382366d766d87)

zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb,zyzyis/monetdb