Pahma 843: Refactored code to use a buffer and add validation