Corrected some part of code. Added some tests.