Add check on loop inheritance. Refactor code. Add tests