updated to start avoiding the reuse of code
re-ordered last two sections
added output