Further refactoring and bug fixing of autoscaler