Program works almost perfectly