Added test and fixed bugs.