Added tests. Corrected some mistakes pointed by Pavel