Fixed few bugs and reformatted query structure