Simplified and fixed the code.