added code from lecture 7 by Ofir Pele