optimized code and cleaned up