PLA-1487 code cleanup & query optimization