Works but needs commenting and prob has memory leakage