Scaling for Spark

Reintegrate the scaling code and fix obvious bugs

Change-Id: I23db6cc77cded53287d81ac3d620a87d7705d340