minor function changes, fixed bugs, added tests