SQL, Code Improvements, Typedefs..

lots of code improvements (performance wise)
fixed few errors
finished statistics stuff w/ SQL
no memory leaks
added comments