fixed the code to be the same as the original scala code