updated code from alex