Updated Scala logic with CursorResult