Added dynamic memory management