hw-4: working implementation, added comments