bugs with memory management. but functional correct.