Fixed overwrite and add refactored code