Export plotting function and many small fixes