reorganized internals, and added test