optimized code and removed unnecessary prints