Modified LU code to handle boundary cases