Cleaned up some things and fixed some bugs.

Signed-off-by: Jacob Duddles <jacob.duddles@live.co.uk>