restructured Marjeta's code into functions