Adjusted to work without dynamic memory allocation (malloc).