added a bit more logic