Tests and code refactoring