Added changes to optimize my sql queries and removed small bugs