updated code, with help of Peck