Improved, cleaned, and formated code