Code changes in this file are shifted over/refactored from ViewModel and modified to make it work as original.