Simplied and optimized code