Refactoring to eliminate dynamic memory allocation