SYS-dbe-39a5.4: Replaced direct queries to `unit` table with function calls. Also some fixes and changes