merged Tommy's code