Refactoring, additional checks and tests