fixed one more bug, but still needs to be optimized