Some refactorings + test - couldnt go to sleep ;)