First working version (still scan and query to do)