Added fixes suggested by @timograham