Added export feature. Cleaned up code.