first working version with joins