Cleaned up the sql extraction code