Slightly simplified the code