1st version inout program
2nd version, optimized
updated comment to state 2nd version