Added memory allocation and state update