Refactored code.

Signed-off-by: Stepan Bujnak <stepanbujnak@fastmail.fm>