Slightly refactors, adds tests and package files.