Bugfixes and optimizations.

Signed-off-by: Difegue <sugoi@cock.li>