Cleaned up the code and made it simpler.