added more code to initialize and finalize