Made much faster, still not working completely though.