Updated the previous patch with better handling.