some bugfixes on database queries