Added bunch of tests. Fixed minor bugs.