Several improvements, see diff