Improve logic and doc strings