Cleaned up and optimized code.