Fixed query and added proper handling for newlines