Note that the code is written in scala.