fixed some severe problems and added tests