Added a test and fixed a bug