First working version with flags