Added test and fixed other bugs