Added test and fixed a bug