[scala] tidy up codes; add comments