refactored to the scala way