Rewritten and optimized