Working version with improvements, too much to count