Written tests and additional refactorization