Added test and fixed minor bugs.