Added latest changes finally, the implementation works but needs much improvement.