Code cleanup and optimisation.

Signed-off-by: crynobone <crynobone@gmail.com>