Clean up and update of the code by Axel Kohlmeyer.