improved working with dynamic memory