Bug fixes - proper SQL query output