Cleanup and refactoring of autoscaler. Perfomance improvements.