Fix remaining bugs, located speed bottleneck