Simplified code and found better boundary fitting