fixed some bugs; still not fast enough