cleaned up sql code  and added ranking method