Fixed bugs + reduced memory requirement