all branches function added and code refactored