updated and optimized