Massive cleanup, refactor and renaming of internals.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>