Optimized code and added test assertions