updated code, most  of it should be the same