Issue #742 fix

SQL queries were in wrong functions