refactor cart function