Improved path handling and query formatting