Refactor export.js and add unit tests