Largely simplifies structure, adds tests