prevent dead code
use order-by correctly
fix the determination of table names