Added test; fixed some bugs