Refactor code to allow library view and to save data in memory