The code has been optimized greatly