improved some sql statements