optimization of knight.rb