now using exec_query subroutine

moved around the code a bit.