refs #81 refactored, commented and cleaned up.