Fixed an error, added more comprehensive tests