Code refactoring, recursive bug fixed