added memory complexity and some changes