Cleaned up and simplified the code.