Moved the code from viewer.js to a separate file