prepare the statements and cleaned code up a bit

prepare sql statements and cleaned the code up a bit.