working version with output