This code is obtained from the LUG library due to Raul Rivero.
