Refatored some parts of "logic" module.