Adding tests, refactoring, improving doco

Signed-off-by: Joshua Paling <joshua.paling@gmail.com>