added some test and a bit of restructuring