Fix bugs, and unit-tests.

Signed-off-by: SharplEr <sharplermc@gmail.com>