final version with output