updated with alexs' code