Added some validation and fixed leak in export