some changes suggested by @saikobee