Modified according to @ntalbott suggestions