added explanation output and optimized