updated according to Ivan's suggestions