merged with thomas' code