working version, included sizes, better memory alloc