added comments where necessary. fixed printing output