Program modified with ref to prajj's concept