Rewrote 2 in scala