functions incapsulated, added comments