Adding more tests and small refactorings throughout the codebase