Refactored getValue code