Fixed seg fault when no records found + some bugs.