Refactors and fixes.

Signed-off-by: King Lemming <kinglemming@gmail.com>