fixed a bug, but now not as efficient