Refactored code. Introduced tests for a subset of core functions