move code around, add tests