added test and fixed bugs