Some refactoring and unit tests

Signed-off-by: Michal Jarzabek <stiopa@gmail.com>