used cody's implementation