Made some modifications to improve speed (gprof)