Problem 31: clean up of scala code