Added input memory value to functions. Refactored code.