Simplified the code slightly