Bug fixes
Bug fixes, refactoring idf function