Adding the reachability function and fixing some bugs