added declarations. printing to err instead of out